
import datetime
from enum import IntEnum
from sqlalchemy import Column, String, Integer, DateTime, Boolean, create_engine, Enum, JSON, DECIMAL, Text, func
from sqlalchemy.dialects.mysql import LONGTEXT
from sqlalchemy.orm import declarative_base, sessionmaker

sync_engine = create_engine("mysql+mysqlconnector://root:new_password@127.0.0.1:3306/slag_test_platform", pool_size=20, max_overflow=10)
# sync_engine = create_engine("mysql+mysqlconnector://remote:new_password@10.10.10.39:3306/slag_test_platform", pool_size=20, max_overflow=10)
# sync_engine = create_engine("mysql+mysqlconnector://root:new_password@mysql:3306/slag_test_platform", pool_size=20, max_overflow=10)
Base = declarative_base()

class BasicsModel():
    id = Column(String(255), primary_key=True)
    remarks = Column(String(255))
    createdAt = Column(DateTime, default=datetime.datetime.now)
    updatedAt = Column(DateTime, default=datetime.datetime.now)

class TurnDict:
    # 单个对象转成map方法1
    def to_dict(self):
        model_dict = dict(self.__dict__)
        del model_dict['_sa_instance_state']
        return model_dict

    Base.to_dict = to_dict

    # 单个对象转成map方法2
    def single_to_dict(self):
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}

    # 多个对象转成map
    def dobule_to_dict(self):
        result = {}
        for key in self.__mapper__.c.keys():
            if getattr(self, key) is not None:
                result[key] = str(getattr(self, key))
            else:
                result[key] = getattr(self, key)
        return result

class User(Base, BasicsModel, TurnDict):
    __tablename__ = 'user'
    nickname = Column(String(255))
    username = Column(String(255))
    password = Column(String(255))
    roleList = []

class Role(Base, BasicsModel, TurnDict):
    __tablename__ = 'role'
    name = Column(String(255))
    menuList = []

class UserRole(Base, BasicsModel):
    __tablename__ = 'user_role'
    userId = Column(String(255))
    roleId = Column(String(255))

class MenuType(IntEnum):
    #文本框
    BOX = 4
    # 按钮
    BUTTON = 3
    # 页面
    ASSEMBLY = 2
    # 目录
    CATALOGUE = 1

class Menu(Base, BasicsModel, TurnDict):
    __tablename__ = 'menu'
    parentId = Column(String(255))
    type = Column(Enum(MenuType))
    name = Column(String(255))
    path = Column(String(255))
    thisRoleMenu = False
    menuList = []
    show = Column(Boolean)
    icon = Column(String(255))
    component = Column(String(255))

class RoleMenu(Base, BasicsModel):
    __tablename__ = 'role_menu'
    roleId = Column(String(255))
    menuId = Column(String(255))

class BackApi(Base, BasicsModel, TurnDict):
    __tablename__ = 'back_api'
    method = Column(String(255))
    path = Column(String(255))

class RoleBackApi(Base, BasicsModel):
    __tablename__ = 'role_back_api'
    roleId = Column(String(255))
    BackApiId = Column(String(255))

class Execut(IntEnum):
    # 未执行
    DEATH = 0
    # 执行中
    ACTIVE = 1

class CaseGroup(Base, BasicsModel, TurnDict):
    __tablename__ = 'case_group'
    title = Column(String(255))
    system_name = Column(String(255))
    modular_name = Column(String(255))
    rerun_times = Column(Integer)
    executing = Column(Enum(Execut))
    case_number = Column(Integer)
    pass_number = Column(Integer)
    fail_number = Column(Integer)

class Status(IntEnum):
    # 禁用
    DISABLE = 0
    # 启用
    VALID = 1

class Case(Base, BasicsModel, TurnDict):
    __tablename__ = 'case'
    case_group_id = Column(String(255))
    title = Column(String(765))
    enable = Column(Enum(Status))
    user_var = Column(Text)
    before_handle = Column(Text)
    method = Column(String(255))
    database_info = Column(Text)
    driver_info = Column(Text)
    header = Column(Text)
    url_or_sql = Column(Text)
    element_info = Column(Text)
    param_type = Column(String(255))
    params = Column(Text)
    file_info = Column(Text)
    extract_var = Column(Text)
    var_name = Column(Text)
    need_data = Column(Text)
    random_info = Column(Text)
    forced_type = Column(Text)
    after_handle = Column(Text)
    assert_info = Column(Text)
    expected_results = Column(Text)
    actual_result_template = Column(Text)
    actual_results = Column(LONGTEXT)
    whether = Column(String(255))

class ScheduledTask(Base, BasicsModel, TurnDict):
    __tablename__ = 'scheduled_task'
    title = Column(String(255))
    enable = Column(Enum(Status))
    execution_info = Column(JSON)
    group_info = Column(String(1275))
    command = Column(String(1275))
    is_batch = Column(Boolean)
    is_cmd = Column(Boolean)

# 配合多个对象使用的函数
def to_json(all_vendors):
    v = [ven.dobule_to_dict() for ven in all_vendors]
    return v

def create_table_all():
    Base.metadata.create_all(sync_engine)

def drop_table_all():
    Base.metadata.drop_all(sync_engine)

SyncSession = sessionmaker(bind=sync_engine)

def get_sync_db():
    return SyncSession()

if __name__ == '__main__':
    create_table_all()

























































